/*********************************************************************************
 *Copyright(C),1996-2022,ChenJiehong
 *FileName:  filter_avg.c
 *Author: ChenJiehong
 *Version:  V1.0
 *Date: 2023-04-23
 ****************************************Includes***********************************/
#include "filter_avg.h"

/***************************************Variables***********************************/

void filter_init(Filter_t *filter)
{
    filter->u16CurrentCode = 0xFFFF;
}

void filter_cal_avg(uint16_t original, uint16_t newData, Filter_t *Filter)
{
    if (original == 0xFFFF)
    {
        for (uint8_t i = 0; i < C_FILTER_BUFF_SIZE; i++)
        {
            Filter->u16Buff[i] = newData;
        }
    }
    else
    {
        for (uint8_t i = 0; i < C_FILTER_BUFF_SIZE - 1; i++)
        {
            Filter->u16Buff[i + 1] = Filter->u16Buff[i];
            Filter->u16Buff[0] = newData;
        }
    }
    Filter->u16Sum = 0;

    for (uint8_t i = 0; i < C_FILTER_BUFF_SIZE; i++)
    {
        Filter->u16Sum += Filter->u16Buff[i];
    }
    Filter->u16CurrentCode = Filter->u16Sum / C_FILTER_BUFF_SIZE;
}

/***************************************Functions***********************************/
